package com.gobuy.business.listener;

import com.gobuy.business.service.Adservice;
import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Gaozexi on 2020/6/7 16:04
 * Step by Step  and Stand on,  You Are The Best Investment!
 */
@Component
public class AdListener {
    //@Autowired
    //private Adservice adservice;

    @Autowired
    private OkHttpClient okHttpClient;

    @RabbitListener(queues = "ad_update_queue")
    public void receiveMessage(String message){
        System.out.println("收到的消息是"+message);
        String updateUrl="http://192.168.170.138/ad_update?position="+message;
        Request request =new Request.Builder().url(updateUrl).build();
        //发送请求
        Call call = okHttpClient.newCall(request);
        //请求回调函数
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求失败
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("广告数据更新成功"+response.message());
            }
        });

    }
}
